package cn.xshi.sys.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.xshi.common.base.*;
import cn.xshi.sys.form.SysPostForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.sys.model.SysPost;
import cn.xshi.sys.service.SysPostService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import javax.annotation.Resource;
/**
 * @Desc 用户岗位
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysPost")
@Api(value = "用户岗位API",tags = "用户岗位API",description = "用户岗位API")
public class SysPostController extends BaseAction {

	@Resource
	private SysPostService sysPostService;

	/**
	 * 查询岗位列表并分页
	 * @param sysPostForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询岗位列表并分页", notes="查询岗位列表并分页")
	@GetMapping(value="/list")
	@Auth(value = "/sysPost/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<SysPost>> getSysPostListByCondition(SysPostForm sysPostForm,Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<SysPost> sysPostList = sysPostService.getSysPostListByCondition(sysPostForm);
		PageInfo<SysPost> page = new PageInfo<SysPost>(sysPostList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	 * 一级岗位
	 * @param departInfoId
	 */
	@ApiOperation(value="一级岗位", notes="一级岗位")
	@GetMapping(value="/one/level/lists/{departInfoId}")
	@Auth(value = "/sysPost/one/level/lists",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<SysPost>> oneLevelLists(@PathVariable("departInfoId")String departInfoId){
		Map<String,Object> condition = new HashMap<>();
		condition.put("departInfoId",departInfoId);
		List<SysPost> sysPosts = sysPostService.getSysPostInfoList(condition);
		return outDataStr(sysPosts);
	}

	/**
	* 查询单个岗位
	* @param id
	*/
	@ApiOperation(value="查询单个岗位", notes="查询单个岗位")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysPost/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysPost> getSysPostById(@PathVariable("id")String id){
		SysPost sysPost = sysPostService.getSysPostById(id);
		if(!StringUtil.isEmpty(sysPost.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysPost.getCreateId());
			if(null != createBy){
				sysPost.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysPost.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysPost.getUpdateId());
			if(null != modifiedBy){
				sysPost.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysPost);
	}
	
	/**
	* 添加
	* @param sysPost
	*/
	@ApiOperation(value="创建单个岗位", notes="创建单个岗位")
	@PostMapping(value="/add")
	public BaseResult addSysPost(@RequestBody SysPost sysPost){
		int i = 0;
		if(null != sysPost){
			sysPost.setId(toUUID());
			if(StringUtil.isEmpty(sysPost.getParentId())){
				sysPost.setParentId("0");
			}
			i= sysPostService.addSysPost(sysPost);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 修改
	* @param sysPost
	*/
	@ApiOperation(value="编辑单个岗位", notes="编辑单个岗位")
	@PutMapping(value="/update")
	public BaseResult updateSysPost(@RequestBody SysPost sysPost){
		int i = 0;
		if(null != sysPost){
			if(StringUtil.isEmpty(sysPost.getParentId())){
				sysPost.setParentId("0");
			}
			i= sysPostService.updateSysPost(sysPost);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 删除
	* @param id
	*/
	@ApiOperation(value="删除岗位", notes="删除岗位")
	@DeleteMapping(value="/delete")
	public BaseResult delSysPost(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysPostService.delSysPost(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	/**
	 * 获取岗位树
	 * @param id
	 */
	@ApiOperation(value="获取岗位树", notes="获取岗位树")
	@GetMapping(value="/tree/{id}")
	@Auth(value = "/sysPost/tree",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult getSysPostTree(@PathVariable("id")String id){
		Map<String, Object> condition = new HashMap<String, Object>();
		List<BaseTreeGridEntity> list = new ArrayList<BaseTreeGridEntity>();
		List<SysPost> sysPostList = sysPostService.getSysPostListAll(condition);
		for(int i = 0; i < sysPostList.size(); i++){
			SysPost sysPost = sysPostList.get(i);
			BaseTreeGridEntity BaseTreeGridEntity = new BaseTreeGridEntity();
			BaseTreeGridEntity.setId(sysPost.getId());
			BaseTreeGridEntity.setPid(sysPost.getParentId());
			BaseTreeGridEntity.setText(sysPost.getName());
			BaseTreeGridEntity.setExpanded(true);
			BaseTreeGridEntity.setSingleClickExpand(true);
			BaseTreeGridEntity.setIcon("/deng/images/icons/target.png");
			list.add(BaseTreeGridEntity);
		}
		BaseTreeGridEntity baseTreeGridEntity = new BaseTreeGridEntity();
		List<BaseTreeGridEntity> baseTreeGridEntityList = baseTreeGridEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseTreeGridEntityList);
		return outStr(json);
	}
	
	/**
	 * 获取岗位树（bootstrap-ztree风格）
	 * @param departInfoId 部门编号
	 */
	@ApiOperation(value="获取岗位树（bootstrap-ztree风格）", notes="获取岗位树（bootstrap-ztree风格）")
	@GetMapping(value="/bztree/{departInfoId}")
	@Auth(value = "/sysPost/bztree",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult getSysPostBZTree(@PathVariable("departInfoId")String departInfoId){
		List<BaseZTreeEntity> list = new ArrayList<BaseZTreeEntity>();
		SysPostForm sysPostForm = new SysPostForm();
		sysPostForm.setDepartInfoId(departInfoId);
		List<SysPost> sysPostList = sysPostService.getSysPostListByCondition(sysPostForm);
		for(int i = 0; i < sysPostList.size(); i++){
			SysPost sysPost = sysPostList.get(i);
			BaseZTreeEntity BaseZTreeEntity = new BaseZTreeEntity();
			BaseZTreeEntity.setId(sysPost.getId());
			BaseZTreeEntity.setPId(sysPost.getParentId());
			BaseZTreeEntity.setText(sysPost.getName());
			BaseZTreeEntity.setName(sysPost.getName());
			BaseZTreeEntity.setExpanded(true);
			BaseZTreeEntity.setSingleClickExpand(true);
			list.add(BaseZTreeEntity);
		}
		BaseZTreeEntity baseZTreeEntity = new BaseZTreeEntity();
		List<BaseZTreeEntity> baseZTreeEntityList = baseZTreeEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseZTreeEntityList);
		return outStr(json);
	}
	
	/**
	 * 根据各种情况查找集合不分页（流程设计器中处理组 发起组等使用）
	 * @param id
	 * @return
	 */
	@ApiOperation(value="根据各种情况查找集合不分页", notes="根据各种情况查找集合不分页")
	@GetMapping(value="/list/{id}")
	@Auth(value = "/sysPost/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<SysPost>> getSysPostList(@PathVariable("id")String id){
		List<SysPost> list = new ArrayList<SysPost>();
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id", id.split(","));
			list = sysPostService.getSysPostList(condition);
		}
		return  outItemsStr(list);
	}
}
